<?php

namespace app\backend\controller;

use app\common\controller\Base;
use app\common\service\sys\UserService;

/**
 * 系统登录
 * @author kaige
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 * @update 2022-04-25
 */
class Login extends Base
{
  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new UserService();
  }

  /**
   * 系统登录
   */
  public function index()
  {
    if (request()->isPost()) {
      //接收数据
      $data = [
        'username' => input('username', ''),
        'password' => input('password', ''),
        'captcha'  => input('captcha', ''),
        'key'      => input('key', '')
      ];


      if (empty($data['username'])) {
        return _jerr(3007);
      }
      if (empty($data['password'])) {
        return _jerr(3008);
      }
      $appid = $this->request->header('Kg-Appid');
      // 登录验证并获取包含访问令牌的用户
      $result = $this->service->login($data,$appid);
      return json($result);
    }
    return _jerr(1000);
  }

  /**
   * 获取基本信息
   */
  public function getinfo()
  {
    $data = [
      'title' => _conf('site_title'),
      'site_name' => _conf('app_name'),
      'app_version' => _conf('app_version'),
      'site_copy' => _conf('site_copy'),
      'miitbeian' => _conf('miitbeian'),
      'site_count_code' => _conf('site_count_code')
    ];
    return _jok('ok',$data);
  }

}
